import { legacy_createStore, applyMiddleware, combineReducers } from "redux";
import { thunk } from "redux-thunk";
function configureStore(options = {}) {
  const { reducer, middleware, preloadedState } = options;
  let rootReducer;

  if (typeof reducer === "function") {
    //   单个 reducer
    rootReducer = reducer;
  } else {
    //   单个 或者 多个 reducer
    rootReducer = combineReducers(reducer);
  }
  // 内置中间件
  const defaultMiddlewares = () => [thunk];
  // 内置中间件 + 自定义中间件
  const middlewares =
    typeof middleware === "function"
      ? middleware(defaultMiddlewares)
      : defaultMiddlewares();
  const store = applyMiddleware(...middlewares)(legacy_createStore)(
    rootReducer,
    // redux 初始值
    preloadedState
  );
  return store;
}
export default configureStore;
